package morningcat.demo2.durable;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import morningcat.MQParams;

/**
 * XXX
 *
 * @author mengzhang6
 * @date 2018/12/17 9:39
 */
public class Sender {

    public static void main(String[] argv) throws Exception {
        // ConnectionFactory
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(MQParams.SERVER_HOST);
        factory.setPort(MQParams.SERVER_PORT);

        // try-with-resources statement
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            // 信道

            // 持久化
            // 当RabbitMQ退出或崩溃时，它将忘记队列和消息，除非你告诉它不要。确保消息不会丢失需要做两件事：我们需要将队列和消息都标记为持久。
            // 首先，我们需要确保RabbitMQ永远不会丢失我们的队列。为此，我们需要声明它是持久的：
            boolean durable = true;
            channel.queueDeclare(MQParams.QUEUE_NAME_DURABLE, durable, false, false, null);

            // durable send
            String message = "你好 RabbitMQ , timestamp:" + System.currentTimeMillis();
            channel.basicPublish("", MQParams.QUEUE_NAME_DURABLE, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"));
        }
    }
}
